package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductService;
import com.goods.common.error.*;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Title:
 * Author: fusu
 * DateTime:  2023/3/24
 * Description:
 **/
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    /**
     * @return com.goods.common.vo.system.PageVO
     * @Author: fusu
     * @Description: 物资资料分页列表
     * @DateTime:18:45 2023/3/24
     * @params: [pageNum, pageSize, name, categorys]
     */
    @Override
    public PageVO findProductList(Integer pageNum, Integer pageSize, String name, List<Long> categorys, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            //名称
            criteria.andLike("name", "%" + name + "%");
        }
        if (!CollectionUtils.isEmpty(categorys)) {
            //categorys不为空
            if (categorys.size() > 0) {
                criteria.andEqualTo("oneCategoryId", categorys.get(0));
            }
            if (categorys.size() > 1) {
                criteria.andEqualTo("twoCategoryId", categorys.get(1));
            }
            if (categorys.size() > 2) {
                criteria.andEqualTo("threeCategoryId", categorys.get(2));
            }
        }
        if (status != null) {
            criteria.andEqualTo("status", status);
        }
        List<Product> products = productMapper.selectByExample(example);
        PageInfo<Product> info = new PageInfo<>(products);
        long total = info.getTotal();
        return new PageVO(total, products);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资资料添加
     * @DateTime:19:55 2023/3/24
     * @params: [productVO]
     */
    @SneakyThrows
    @Override
    public void addProduct(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        if (categoryKeys.length != 3) {
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR, "[物资资料添加]三级分类参数有误");
        }
        product.setOneCategoryId(categoryKeys[0]);
        product.setTwoCategoryId(categoryKeys[1]);
        product.setThreeCategoryId(categoryKeys[2]);
        String pNum = "FUSU:" + UUID.randomUUID().toString().replaceAll("-", "");
        product.setPNum(pNum);
        product.setStatus(2);
        productMapper.insert(product);
    }

    /**
     * @return com.goods.common.model.business.Product
     * @Author: fusu
     * @Description: 物资资料根据id获取
     * @DateTime:20:21 2023/3/24
     * @params: [productId]
     */
    @Override
    public Product getByProductId(Long productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        return product;
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资资料根据id修改
     * @DateTime:20:25 2023/3/24
     * @params: [product]
     */
    @Override
    public void updateByProductId(Product product) {
        product.setModifiedTime(new Date());
        productMapper.updateByPrimaryKey(product);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资资料根据id放入回收站
     * @DateTime:20:28 2023/3/24
     * @params: [productId]
     */
    @Override
    public void removeByProductId(Long productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product != null) {
            product.setModifiedTime(new Date());
            product.setStatus(1);
            productMapper.updateByPrimaryKey(product);
        }
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资资料根据id删除
     * @DateTime:20:47 2023/3/24
     * @params: [productId]
     */
    @Override
    public void deleteByProductId(Long productId) {
        productMapper.deleteByPrimaryKey(productId);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资资料根据id恢复
     * @DateTime:20:49 2023/3/24
     * @params: [productId]
     */
    @Override
    public void bactProduct(Long productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product != null) {
            product.setModifiedTime(new Date());
            product.setStatus(0);
            productMapper.updateByPrimaryKey(product);
        }
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资资料根据id通过
     * @DateTime:21:08 2023/3/24
     * @params: [productId]
     */
    @Override
    public void publishProduct(Long productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product != null) {
            product.setModifiedTime(new Date());
            product.setStatus(0);
            productMapper.updateByPrimaryKey(product);
        }
    }

    /**
     * @return java.util.List<com.goods.common.vo.business.ProductVO>
     * @Author: fusu
     * @Description: 分页获取物资库存信息
     * @DateTime:14:18 2023/3/27
     * @params: [pageNum, pageSize, name, categorys]
     */
    @Override
    public PageVO findAllStocks(Integer pageNum, Integer pageSize, String name, List<Long> categorys) {
        PageVO pageVO = this.findProductList(pageNum, pageSize, name, categorys, 0);
        if (pageVO != null) {
            List<Product> products = pageVO.getRows();
            if (!CollectionUtils.isEmpty(products)) {
                List<ProductStockVO> productStockVOS = products.stream().map(product -> {
                    ProductStockVO productStockVO = new ProductStockVO();
                    BeanUtils.copyProperties(product, productStockVO);
                    Example example = new Example(ProductStock.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("pNum", product.getPNum());
                    ProductStock productStock = productStockMapper.selectOneByExample(example);
                    productStockVO.setStock(productStock.getStock());
                    return productStockVO;
                }).collect(Collectors.toList());
                PageVO<ProductStockVO> pageVoStock = new PageVO<>(pageVO.getTotal(), productStockVOS);
                return pageVoStock;
            }
        }
        return null;
    }

}
